package com.example.case02_recyclerview229;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class Adapter extends RecyclerView.Adapter {
    private Context context;
    private List<Food_bhm229> list;
    private LayoutInflater layoutInflater;
    public  Adapter(Context context,List<Food_bhm229> list){
        this.context=context;
        this.list=list;
        layoutInflater=LayoutInflater.from(context);
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v=layoutInflater.from(layoutInflater.getContext()).inflate(R.layout.item,parent,false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
       final Food_bhm229 bean=(Food_bhm229) list.get(position);
        if(bean==null) return;
        ViewHolder viewHolder=(ViewHolder)holder;
        viewHolder.imageView.setImageResource(bean.getImages());
        viewHolder.tvtime.setText(bean.getTime());
        viewHolder.tvtitle.setText(bean.getTitle());
        viewHolder.tvdes.setText(bean.getDes());
        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, FoodActivity_229.class);
//                intent.putExtra("img",bean.setImages());
                intent.putExtra("food_title",bean.getTitle());
                intent.putExtra("food_pl",bean.getPl());
                intent.putExtra("food_price",bean.getPrice());
                intent.putExtra("food_tel",bean.getTel());
                intent.putExtra("img",bean.getImages());
                intent.putExtra("name",bean.getShopname());
                context.startActivity(intent);
            }
        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,bean.getTitle()+"    "+bean.getShopname()+" "+bean.getDes()+"     "+bean.getPl()+"  "+bean
                        .getTime(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public int getItemCount() {
        if(list!=null)
            return list.size();
        else
          return 0;
    }
    public  class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView tvtitle,tvtime,tvdes;
        Button button;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imageView);
            tvtitle=itemView.findViewById(R.id.textView);
            tvdes=itemView.findViewById(R.id.textView2);
            tvtime=itemView.findViewById(R.id.textView3);
            button=itemView.findViewById(R.id.button);
        }
    }
}
